home *** CD-ROM | disk | FTP | other *** search
- /* Functions for isolating the application from graphics devices, so that
- the same code can be used on macs that support graphics devices as well
- as macs that don't support them. Also, the common user interface tasks
- of determining which screen to position a window on are handled quite
- simply using this library. The function ScreenGet returns the library's
- concept of the current screen and the function ScreenRect returns the
- current screen's bounding rectangle, while ScreenGrayRect returns the
- gray area (i.e., excluding the menu bar if on the main screen). To set
- the current screen so that the next call to ScreenRect will return the
- bounding rectangle of the screen containing the frontmost window simply
- use
-
- ScreenSet(ScreenContainingWindow(window));
-
- Similarly, to ensure that the next window (e.g., alert) created will be on
- the screen where the last mouse down occurred use
-
- ScreenSet(ScreenContainingPt(where));
-
- Since the window library uses the function ScreenGrayRect when positioning
- windows at their default position and size these functions help ensure
- compliance with the user interface guidlines for single and multiple
- screen macs.
-
- Portions based on the C/Shell application distributed on the System 7.0
- Golden Release CD-ROM.
-
- Revisions:
-
- 93/12/16 aih
- - fixed bug if there was no current screen (defaults to main screen)
-
- 93/12/05 aih
- - fixed function for calculating screen region
- - added function for calculating screen's gray rectangle
-
- 93/03/19 AIH
- - Changed ScreenLibBegin to ScreenInit
-
- 91/07/02 AIH
- - Added some assertions and comments
-
- 91/05/12 Ari Halberstadt (AIH)
- - Created this library. */
-
- #include <stddef.h>
- #include "MacLib.h"
- #include "ScreenLib.h"
-
- /* the current screen */
- static ScreenHandle gScreen;
-
- /* true if the screen is a valid screen */
- Boolean ScreenValid(ScreenHandle screen)
- {
- Boolean result = false;
-
- if (MacHasSingleScreen())
- result = (screen == NULL);
- else
- result = (screen != NULL);
- return(result);
- }
-
- /* return the current screen */
- ScreenHandle ScreenGet(void)
- {
- require(! gScreen || ScreenValid(gScreen));
- return(gScreen ? gScreen : GetMainDevice());
- }
-
- /* set the current screen, or do nothing if NULL */
- void ScreenSet(ScreenHandle screen)
- {
- require(! screen || ScreenValid(screen));
- if (screen)
- gScreen = screen;
- }
-
- /* Get region enclosing all of the screens. Needed since GetGrayRgn doesn't
- include the menu bar. */
- const RgnHandle ScreenRgnAll(void)
- {
- static RgnHandle screenRgn;
- Rect gdRect = screenBits.bounds;
-
- if (! screenRgn) {
- screenRgn = NewRgn();
- FailNIL(screenRgn);
- if (MacHasMultipleScreens())
- gdRect = (**GetMainDevice()).gdRect;
- RectRgn(screenRgn, &gdRect);
- UnionRgn(screenRgn, GetGrayRgn(), screenRgn);
- }
- return(screenRgn);
- }
-
- /* return the bounding rectangle of the current screen */
- void ScreenRect(Rect *r)
- {
- *r = screenBits.bounds;
- if (MacHasMultipleScreens())
- *r = (**ScreenGet()).gdRect;
- /* the rectangle must be at least the size of the original mac screen */
- ensure(r->right - r->left >= 512 && r->bottom - r->top >= 342);
- }
-
- /* return bounding rectangle of screen's gray region */
- void ScreenGrayRect(Rect *gray)
- {
- ScreenRect(gray);
- if (ScreenHasMenuBar())
- gray->top += GetMBarHeight();
- }
-
- /* true if the current screen contains the menu bar */
- Boolean ScreenHasMenuBar(void)
- {
- return(MacHasSingleScreen() || ScreenGet() == GetMainDevice());
- }
-
- /* Return the screen containing the point. The point should be in global
- coordinates. */
- ScreenHandle ScreenContainingPt(Point pt)
- {
- GDHandle gd; /* current device */
- ScreenHandle screen = NULL; /* screen to return */
-
- if (MacHasMultipleScreens()) {
- for (gd = GetDeviceList(); gd && ! screen; gd = GetNextDevice(gd)) {
- if (TestDeviceAttribute(gd, screenDevice) &&
- TestDeviceAttribute(gd, screenActive) &&
- PtInRect(pt, &(**gd).gdRect))
- {
- screen = gd;
- }
- }
- }
- return(screen);
- }
-
- /* Return the screen containing the largest portion of the rectangle,
- or NULL if the rectangle doesn't intersect any screen or the macintosh
- doesn't support multiple screens. The rectangle should be in global
- coordinates. */
- ScreenHandle ScreenContainingRect(Rect *r)
- {
- Rect sect; /* intersection of rectangle and device */
- long area; /* area of intersection */
- long maxArea; /* largest intersection found */
- GDHandle gd; /* device being checked */
- ScreenHandle screen = NULL; /* screen to return */
-
- if (MacHasMultipleScreens()) {
- maxArea = 0;
- for (gd = GetDeviceList(); gd; gd = GetNextDevice(gd)) {
- if (TestDeviceAttribute(gd, screenDevice) &&
- TestDeviceAttribute(gd, screenActive) &&
- SectRect(r, &(**gd).gdRect, §))
- {
- area = (sect.right - sect.left) * (sect.bottom - sect.top);
- if (area > maxArea) {
- screen = gd;
- maxArea = area;
- }
- }
- }
- }
- return(screen);
- }
-
- /* Return the screen containing the largest portion of the window,
- or NULL if either the window is invisible, or the macintosh
- doesn't support multiple screens, or if the window is NULL.
- This makes it simple to set the current screen:
-
- ScreenSet(ScreenContainingWindow(FrontWindow()))
-
- The current screen won't be changed if there is no front window. */
- ScreenHandle ScreenContainingWindow(WindowPtr window)
- {
- GrafPtr port;
- Rect portRect;
- ScreenHandle screen = NULL;
-
- if (window) {
- portRect = window->portRect;
- GetPort(&port);
- SetPort(window);
- LocalToGlobal(&((Point *) &portRect)[0]);
- LocalToGlobal(&((Point *) &portRect)[1]);
- SetPort(port);
- screen = ScreenContainingRect(&portRect);
- }
- return(screen);
- }
-
- /* initialize the screen library */
- void ScreenInit(void)
- {
- gScreen = NULL;
- if (MacHasMultipleScreens())
- gScreen = GetMainDevice();
- }
-